/* $Id: overlay.jquery.js,v 1.1.2.3 2008/07/01 20:04:42 gburns Exp $

Overview:
	jQuery.overlay

Parameters:
	prepend: (String) String to prepend classes/id's
		Default: 'modal'
	opacity: (Float) Opacity for overlay
		Default: 0.8
	speed: (String) Animation speed for modal animations
		Default: 'slow'
	closeOnClick: (Boolean) Close the modal if user clicks
		the background.
		Default: true
	zIndex: (Float) z-index of overlay/modal
		Default 10000

Usage:
	JavaScript:
	$.overlay.show();

Author:
	gburns

TODO:
	documentation
*/

(function($) {
var Overlay = function(options) {
	var showing = false;
	var closing = false;
	var me = this;
	var $body = $('body');
	var $window = $(window);
	var closing = false;

	this.settings = options;

	var click_close = function() {
		if ( me.settings.closeOnClick && !closing ) me.hide();
	};

	var $object = $('<div/>')
		.attr({id: me.settings.prepend + 'overlay_object'})
		.addClass('overlay_object')
		.css({
			position: 'absolute',
			top: 0,
			left: 0,
			opacity: 0,
			display: 'none',
			zIndex: me.settings.zIndex
			})
		.bind('click', click_close)
		.appendTo($body);

	this.object = $object;

	this.resize = function() {
		var width = $(document).width();
		var height = $(document).height();

		$object
			.css({
				width: width,
				height: height
				});
	};

	this.hide = function(options) {
		if ( closing ) return;
		options = $.extend(me.settings, options || {});
		if ( options.speed ) {
			closing = true;
			$object.animate({opacity: 0}, options.speed, null, function() {
				closing = false;
				$object.hide();
				if ( me.settings.closeCallback ) me.settings.closeCallback();
			});
		} else {
			$object
				.css({opacity: 0})
				.hide();

			if ( me.settings.closeCallback ) me.closeCallback();
		}

		var $hide_objects = $('iframe[overlay_hide="true"], object[overlay_hide="true"], embed[overlay_hide="true"], select[overlay_hide="true"]');

		$hide_objects.each(function() {
			$this = $(this);
			$this
				.attr({
					modal_hide: null
					//src: $this.attr('modal_src'),
					//modal_src: null
					})
				.css({visibility: 'visible'});
		});

		$window.unbind('resize', me.resize);
	};

	this.show = function(options) {
		options = $.extend(me.settings, options || {});

		var $hide_objects = $('iframe:visible, object:visible, embed:visible, select:visible');

		$hide_objects.each(function() {
			var $this = $(this);
			$this
				.attr({
					overlay_hide: 'true'
					//modal_src: $this.attr('src'),
					//src: null
					})
				.css({visibility: 'hidden'});
		});

		if ( closing ) {
			$object.stop(true);
			closing = false;
		} else {
			$object
				.css({
					opacity: 0
					})
				.show();
		}

		me.resize();

		if ( options.speed ) {
			$object.animate({opacity: options.opacity}, options.speed);
		} else {
			$object.css({opacity: options.speed});
		}

		$window.bind('resize', me.resize);
	};
};

$.overlay = {
	settings: {
		prepend: 'overlay_',
		opacity: 0.8,
		speed: 'slow',
		closeOnClick: true,
		zIndex: 10000,
		closeCallback: null
	},

	create: function(options) {
		var options = $.extend(this.settings, options || {});
		var name = 'jQuery::overlay::' + options.prepend;
		var overlay = $.data(document.body, name);
		if ( !overlay ) {
			overlay = new Overlay(options);
			$.data(document.body, name, overlay);
		}
		return overlay;
	},

	show: function(options) {
		var overlay = this.create(options);
		overlay.show();
		return overlay;
	}
};
})(jQuery);